package com.sicnu.cheer.leaugebar.leaguemodle.activity;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sicnu.cheer.generalmodule.activity.BaseActivity;
import com.sicnu.cheer.leaugebar.leaguemodle.R;
import com.sicnu.cheer.leaugebar.leaguemodle.wigets.MyViewPager;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * 显示大图，手势切换放大缩小等
 */
public class BigPhotoActivity extends BaseActivity {
    private List<View> content;//加载显示内容
    private MyPagerAdapter adapter;
    private MyViewPager viewPager;
    private LayoutInflater inflater;

    private ArrayList<String> imgList;
    private TextView curr_page_txt;

    @Override
    protected void initView() {
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        ((TextView) findViewById(R.id.title)).setText("图片查看");
        viewPager = (MyViewPager) findViewById(R.id.capture_vp);
        curr_page_txt = ((TextView) findViewById(R.id.curr_page));
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        imgList = (ArrayList<String>) intent.getSerializableExtra("list");
        inflater = LayoutInflater.from(this);
        adapter = new MyPagerAdapter();
        content = new ArrayList<>();
        viewPager.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        viewPager.setCurrentItem(intent.getIntExtra("imgPosition", 0));

        for (int i = 0; i < imgList.size(); i++) {
            View view = inflater.inflate(R.layout.item_big_photo, null);
            content.add(view);
        }
        //初始化图片下面的信息
        curr_page_txt.setText(viewPager.getCurrentItem() + 1 + "/" + imgList.size());
        initListener();
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_big_photo;
    }

    private void initListener() {
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //在一个页面被选中的时候更新
                curr_page_txt.setText(viewPager.getCurrentItem() + 1 + "/" + imgList.size());
                View view = content.get(position);
                PhotoView photoView = (PhotoView) view.findViewById(R.id.ivPhoto);
                PhotoViewAttacher photoViewAttacher = new PhotoViewAttacher(photoView);
                photoViewAttacher.getDisplayMatrix().reset();

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }




    class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imgList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        /**
         * viewpager的初始化操作
         *
         * @param container viewPager的容器
         * @param position  当前page的位置
         * @return
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = content.get(position);
            final ImageView imageView = (ImageView) view.findViewById(R.id.ivPhoto);
            Glide.with(mThis).load(imgList.get(position)).into(imageView);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(content.get(position));
        }
    }

}
